﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AutomatedTP
{
    /// <summary>
    /// Data class for holidng all information about test procedure.
    /// </summary>
    public class TP
    {
        private char analysis_level;
        private bool timing;
        private bool manual;
        private string module;
        private string name;        

        /// list of global counters for instances!
        private List<string> glob_cnt = new List<string>();

        /// collection of test cases!!!!!
        private List<TC> test_cases = new List<TC>();

        /// collection of local functions!
        private List<Func> local_func = new List<Func>();

        private List<Variable> registers = new List<Variable>();
        private List<Variable> glob_var = new List<Variable>();

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <returns>Return name of test procedure</returns>
        public string GetName()
        {
            return name;
        }

        /// <summary>
        /// Gets the analysis_level.
        /// </summary>
        /// <returns>Return level of analysis</returns>
        public char GetAnalysis_level()
        {
            return analysis_level;
        }

        /// <summary>
        /// Gets the timing.
        /// </summary>
        /// <returns>Return <c>true</c> if timing is enabled</returns>
        public bool GetTiming()
        {
            return timing;
        }


        /// <summary>
        /// Gets the manual.
        /// </summary>
        /// <returns>Return <c>true</c> if manual mode is enabled</returns>
        public bool GetManual()
        {
            return manual;
        }

        /// <summary>
        /// Gets the module.
        /// </summary>
        /// <returns>Return module</returns>
        public string GetModule()
        {
            return module;
        }

        /// <summary>
        /// Gets the list of local functions.
        /// </summary>
        /// <returns></returns>
        public List<Func> GetLfunc()
        {
            return local_func;
        }

        /// <summary>
        /// Gets the list of test cases.
        /// </summary>
        /// <returns></returns>
        public List<TC> GetTC()
        {
            return test_cases;
        }

        /// <summary>
        /// Gets the global counters for checking instatnces.
        /// </summary>
        /// <returns></returns>
        public List<string> GetCounters()
        {
            return glob_cnt;
        }

        /// <summary>
        /// Gets the list of global registers.
        /// </summary>
        /// <returns></returns>
        public List<Variable> GetRegisters()
        {
            return registers;
        }

        /// <summary>
        /// Gets the list of global variables.
        /// </summary>
        /// <returns></returns>
        public List<Variable> GetGVar()
        {
            return glob_var;
        }

        /// <summary>
        /// Sets the timing.
        /// </summary>
        /// <param name="value">if set to <c>true</c> timing is anabled [value].</param>
        public void SetTiming(bool value)
        {
            timing = value;
        }

        /// <summary>
        /// Sets the manual.
        /// </summary>
        /// <param name="value">if set to <c>true</c> if manual is enabled [value].</param>
        public void SetManual(bool value)
        {
            manual = value;
        }

        /// <summary>
        /// Sets the name of module.
        /// </summary>
        /// <param name="module">The module.</param>
        public void SetModule(string module)
        {
            this.module = module;
        }

        /// <summary>
        /// Sets the level of analysis for code coverage.
        /// </summary>
        /// <param name="level">The level.</param>
        public void SetLevel(char level)
        {
            analysis_level = level;
        }

        /// <summary>
        /// Sets the name of test procedure.
        /// </summary>
        /// <param name="name">The name.</param>
        public void SetName(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// Adds the test case to the list of all test cases in this test procedure.
        /// Test case should be from same module as other test cases!
        /// </summary>
        /// <param name="testCase">The test case to be added.</param>
        public void AddTestCase(TC testCase)
        {             
            test_cases.Add(testCase);
        }
        
        /// <summary>
        /// Adds the glob count. Add only if given string is not contained in glob_cnt
        /// </summary>
        /// <param name="cnt">The count.</param>
        public void AddGlobCnt(string cnt)
        {            
            if(!glob_cnt.Contains(cnt))
                glob_cnt.Add(cnt);
        }

        /// <summary>
        /// Adds to the list of glob variable.
        /// </summary>
        /// <param name="var">The variable to be added.</param>
        public void AddGlobVar(Variable var)
        {
            if ("gvar".Equals(var.form))
            {
                if (!glob_var.Exists(t => t.name.Equals(var.name)))
                    glob_var.Add(var);
            }
            else
            {
                Logger.WriteToLog(3, "Error in parsing. This is not global variable! " + var.name);
            }
        }

        /// <summary>
        /// Adds to the list of global registers.
        /// </summary>
        /// <param name="var">The variable.</param>
        public void AddRegister(Variable var)
        {
            if ("reg".Equals(var.form))
            {
                if(!registers.Exists(t => t.name.Equals(var.name)))
                    registers.Add(var);
            }
            else
            {
                Logger.WriteToLog(3, "Error in parsing. This is not register! " + var.name);
            }
        }

        /// <summary>
        /// Adds parameter to the list of local functions.
        /// </summary>
        /// <param name="func">The function.</param>
        public void AddLocal(Func func)
        {
            bool contain = false;
            foreach (var tmp in local_func)
            {
                if (tmp.name.Equals(func.name))
                {
                    contain = true;
                    break;
                }
            }
            if (!contain)
            {
                local_func.Add(func);
            }
        }
    }
}
